/*
 * SignModel.java
 * business
 *
 * Created by SunDing on 2016/12/29.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.response.RaffleRecordResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.SignHistoryListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 签到抽奖相关
 */
public class SignModel implements ISignModel {
    private NetBiz mNetBiz = new NetBiz();
    private List<DisplayListItem> recordLists = new ArrayList<>();
    private int page;

    @Override
    public void getRaffleRecord(
            String tag, boolean isRefresh, final onLoadRecordListener listener) {
        if (isRefresh) {
            page = 1;
            recordLists.clear();
        }
        PagerRequest request = new PagerRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DRAW_RECORD);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(page);
        mNetBiz.sendGetRequest(request, RaffleRecordResponse.class,
                new NetListener<RaffleRecordResponse>() {
                    @Override
                    public void onSuccess(RaffleRecordResponse response) {
                        page++;
                        RaffleRecordResponse.DataBean data = response.getData();
                        String count = null;
                        if (null != data) {
                            count = data.getCount();
                            List<RaffleRecordResponse.DataBean.DrawRecordBean> drawRecord =
                                    data.getDrawRecord();
                            if (null != drawRecord) {
                                ArrayList<DisplayListItem> items = new ArrayList<>();
                                for (int i = 0; i < drawRecord.size(); i++) {
                                    items.add(new SignHistoryListItem(drawRecord.get(i)));
                                }
                                recordLists.addAll(items);
                            }
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    recordLists,
                                    count,
                                    recordLists.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
